// lib/src/ui/calendar_header.dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:omni_calendar_view/src/controller/omni_calendar_controller.dart';
import 'package:omni_calendar_view/src/ui/custom_switch.dart';
import 'package:omni_calendar_view/src/utils/localization_util.dart';

class CalendarHeader extends StatelessWidget {
  final OmniCalendarController controller;
  final VoidCallback? onHeaderTap;
  final Locale locale;
  final VoidCallback onPreviousMonthPressed;
  final VoidCallback onNextMonthPressed;

  const CalendarHeader({
    super.key,
    required this.controller,
    required this.locale,
    this.onHeaderTap,
    required this.onPreviousMonthPressed,
    required this.onNextMonthPressed,
  });

  @override
  Widget build(BuildContext context) {
    // 使用 AnimatedBuilder 监听控制器变化
    return AnimatedBuilder(
      animation: controller,
      builder: (context, _) {
        return Column(
          children: [
            _buildTopBar(context),
            const SizedBox(height: 12),
          ],
        );
      },
    );
  }

  Widget _buildTopBar(BuildContext context) {
    final localizations = OmniLocalizations(locale);
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        _buildLeftSection(localizations),
        _buildRightSection(localizations),
      ],
    );
  }

  Widget _buildLeftSection(OmniLocalizations localizations) {
    return Row(
      crossAxisAlignment: CrossAxisAlignment.center,
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Row(
          children: [
            IconButton(
              onPressed: onPreviousMonthPressed,
              icon: Icon(Icons.chevron_left),
            ),
            GestureDetector(
              onTap: onHeaderTap,
              child: Text(
                localizations.getYearMonth(
                    controller.displayedDate.year, controller.displayedDate.month),
                style: const TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
              ),
            ),
            IconButton(
              onPressed:onNextMonthPressed,
              icon: Icon(Icons.chevron_right),
            ),
          ],
        ),

        // 仅在中文环境下显示开关
        if (localizations.isChinese) ...[
          Row(
            children: [
              CustomSwitch(
                value: controller.showLunar,
                onChanged: (value) => controller.toggleLunar(value),
              ),
              const SizedBox(width: 8,),
              Text(localizations.lunarLabel, style: TextStyle(fontSize: 12 , fontWeight: FontWeight.bold )),
            ],
          )
        ]
      ],
    );
  }

  Widget _buildRightSection(OmniLocalizations localizations) {
    if (controller.selectionType == SelectionType.range &&
        controller.selectedDateRange != null) {
      final range = controller.selectedDateRange!;
      return Row(
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          Text(
            '${range.start.year}-${range.start.month.toString().padLeft(2, '0')}-${range.start.day.toString().padLeft(2, '0')}',
            style:  TextStyle(fontSize: 14, color: Colors.black , fontWeight: FontWeight.bold ),
          ),
          if (range.end != range.start)...[
            Icon(Icons.arrow_right_alt_rounded,size: 16,),
            Text(
              '${range.end.year}-${range.end.month.toString().padLeft(2, '0')}-${range.end.day.toString().padLeft(2, '0')}',
              style: TextStyle(fontSize: 14, color: Colors.black, fontWeight: FontWeight.bold ),
            ),
          ]

        ],
      );
    }

    if (controller.selectedDate != null) {
      final date = controller.selectedDate!;
      return Text(
        localizations.getFormattedDate(date),
        style: TextStyle(fontSize: 14, color: Colors.black, fontWeight: FontWeight.bold),
      );
    }

    return const SizedBox.shrink();
  }
}
